  set more off
  mat drop _all
  set seed 19112014

*===================================================================*
*   BIHAR EVALUATION OF SOCIAL FRANCHISING AND TELEMEDICINE (BEST)
*                           ANCOVA ANALYSIS
*====================================================================*

*====================================================================*
* TABLE A7
*====================================================================*

*====================================================================*
* Diarrhea
*====================================================================*

* Append Datasets
  use "$prodata1\providers_interview_1st", clear
  merge 1:1 prov_id wave using "$prodata1\vignette_diarrhea", keepus(case sample4 nhistory dx_type tx_any tx_detail tx_type vig_theta_mle_q12_std vig_item? vig_item??)
  drop _merge

  append using "$prodata2\providers_interview_2nd"
  merge 1:1 prov_id wave using "$prodata2\vignette_diarrhea", update keepus(case sample4 nhistory dx_type tx_any tx_detail tx_type vig_theta_mle_q12_std vig_theta_mle_q12_stdb vig_item? vig_item??)
  drop _merge

* Sample (see do file 3_samples)
  sort wave prov_id
  merge 1:1 wave prov_id using "$prodata\sample_by_wave"
  keep if _merge==3
  drop _merge 
  assert _N==810

* Sample selection 2
/*NOTE: we need to exclude WHP providers*/
  gen     regsample = 1
  replace regsample = 0 if WHP_oversampled==1

* Post variable
  gen post = (wave==2)

* Percentage of selected items asked
  egen pq_vig_d=rmean(vig_item1-vig_item10 vig_item12-vig_item14)

* Unnecessary and harmful treatments 
  gen tx_harm=(tx_detail>=3 & tx_detail<=5 | tx_detail==7)

* ORS
  gen     ORS = (tx_detail<=5)
  lab var ORS "=1 if ORS given"

* Knowledge Score
  gen     knowscore = vig_theta_mle_q12_std if wave==1
  replace knowscore = vig_theta_mle_q12_stdb if wave==2

* Dependent variable
  local dep = "pq_vig_d dx_type ORS tx_type tx_harm"

* Outcome means at wave 1
  foreach var of local dep {
    bys cluster: egen aux`var'=mean(`var') if wave==1
    bys cluster: egen m`var'=min(aux`var')
    drop aux`var'
  }

* Regressors
/*(NOTE: post variable do not have sense since I am restricting the regression to wave=2)*/
  local reg1 "post treat"
  local reg2 "post treat age experience mqualif knowscore"
  local reg3 "post treat age experience mqualif knowscore work_hs npatient camps fac_gov niindex"

* Matrix to store results
  mat A7_vignette = J(11,8,.)
  mat colnames A7_vignette = N mean beta se ci_l ci_u pvalue adj_pvalue

* Regressions
  local i 1
  foreach var of local dep {
    forvalues r=3/3 {

    * Mean value of dependent variable 
      sum `var' if regsample==1
      local mean = string(round(r(mean)*100, .1))

    * Regressions
      areg `var' m`var' `reg`r'' if regsample==1 & wave==2, absorb(district) cluster(cluster)

    * Fill matrix
      if `r'==3 {
        mat A7_vignette[`i',1] = e(N)
        mat A7_vignette[`i',2] = `mean'
        mat A7_vignette[`i',3] = _b[treat]
        mat A7_vignette[`i',4] = _se[treat]
        local low_ci  = _b[treat] - invttail(e(df_r),0.025)*_se[treat]
        local high_ci = _b[treat] + invttail(e(df_r),0.025)*_se[treat]
        mat A7_vignette[`i',5] = `low_ci'
        mat A7_vignette[`i',6] = `high_ci'
        local t = _b[treat]/_se[treat]
        mat A7_vignette[`i',7] = 2*ttail(e(df_r),abs(`t'))
      }

    * Save results
      if "`dep'"=="pq_vig_d" & `r'==1 local app = "replace"
      else local app = "append"
      outreg2 using "$results\A7_diarrhea_vignette.xls", keep(treat) ctitle(`var') bdec(3) sdec(3) ///
      nocons stats(coef ci_low ci_high pval) noparen addstat("Mean", `mean') addnote("District FE") `app'
    }
  local i = `i'+1
  }


* Erase txt files created by outreg
  erase "$results\A7_diarrhea_vignette.txt"




*====================================================================*
* Pneumonia
*====================================================================*

* Append Datasets
  use "$prodata1\providers_interview_1st", clear

  count
  merge 1:1 prov_id wave using "$prodata1\vignette_pneumonia", keepus(case sample4 nhistory dx_type tx_any tx_detail tx_type vig_theta_mle_q12_std vig_item? vig_item??)

  keep if _merge==3
  tab sample1 sample4
  drop sample4
  drop _merge

  append using "$prodata2\providers_interview_2nd"
  merge 1:1 prov_id wave using "$prodata2\vignette_pneumonia", update keepus(case sample4 nhistory dx_type tx_any tx_detail tx_type vig_theta_mle_q12_std vig_theta_mle_q12_stdb vig_item? vig_item??)
  tab sample1 sample4 if wave==2

* Sample selection
  keep if wave==1 | (wave==2 & sample1==1 & sample4==1)

* Sample selection 2
/*NOTE: we need to exclude WHP providers*/
  gen     regsample = 1
  replace regsample = 0 if WHP_oversampled==1

* Post variable
  gen post = (wave==2)

* Percentage of selected items asked
  egen pq_vig_p=rmean(vig_item1-vig_item9)

* Unnecessary and harmful treatments 
  gen tx_harm=(tx_detail>=3 & tx_detail<=5)

* Harmful or non treatment
  gen tx_harm_no=(tx_detail>=3 & tx_detail<=6)

* Antibiotics
  gen antibiotics=(tx_detail<=4)

* Knowledge Score
  gen     knowscore = vig_theta_mle_q12_std if wave==1
  replace knowscore = vig_theta_mle_q12_stdb if wave==2

* Dependent variable
  local dep = "pq_vig_p dx_type antibiotics tx_type tx_harm tx_harm_no"
  local dep = "pq_vig_p dx_type antibiotics tx_type tx_harm"

* Outcome means at wave 1
  foreach var of local dep {
    bys cluster: egen aux`var'=mean(`var') if wave==1
    bys cluster: egen m`var'=min(aux`var')
    drop aux`var'
  }

* Regressors
  local reg1 "post treat"
  local reg2 "post treat age experience mqualif knowscore"
  local reg3 "post treat age experience mqualif knowscore work_hs npatient camps fac_gov niindex"

* Regressions
  local i 7
  foreach var of local dep {
    forvalues r=1/3 {
      
    * Mean value of dependent variable 
      sum `var' if regsample==1
      local mean = string(round(r(mean)*100, .1))

    * Regressions
      areg `var' m`var' `reg`r'' if regsample==1 & wave==2, absorb(district) cluster(cluster)

    * Fill matrix
      if `r'==3 {
      mat A7_vignette[`i',1] = e(N)
      mat A7_vignette[`i',2] = `mean'
      mat A7_vignette[`i',3] = _b[treat]
      mat A7_vignette[`i',4] = _se[treat]
      local low_ci  = _b[treat] - invttail(e(df_r),0.025)*_se[treat]
      local high_ci = _b[treat] + invttail(e(df_r),0.025)*_se[treat]
      mat A7_vignette[`i',5] = `low_ci'
      mat A7_vignette[`i',6] = `high_ci'
      local t = _b[treat]/_se[treat]
      mat A7_vignette[`i',7] = 2*ttail(e(df_r),abs(`t'))
      }

    * Save results
      if "`dep'"=="pq_vig_p" & `r'==1 local app = "replace"
      else local app = "append"
      outreg2 using "$results\A7_pneumonia_vignette.xls", keep(treat) ctitle(`var') bdec(3) sdec(3) ///
      nocons stats(coef ci_low ci_high pval) noparen addnote("District FE") addstat("Mean", `mean') `app'
    }
  local i = `i'+1
  }

* Erase txt files created by outreg
  erase "$results\A7_pneumonia_vignette.txt"



* Export tables
  preserve
  drop _all
  svmat2 A7_vignette, names(col)
  foreach var of varlist beta-adj_pvalue {
    replace `var' = round(`var',.001)
  }
  outsheet using "$results\A7_vignette.xls", replace
  restore
